package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.net.ConnectException;

import ch.rheintalconnection.sysventory.dto.ProcessorDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ProcessorScan {

	private ActiveXComponent axWMI;
	private String host;
	
	public static final String QUERY = "SELECT * FROM Win32_Processor";

	public ProcessorScan(ActiveXComponent axWMI, String host) {
		super();
		this.axWMI = axWMI;
		this.host = host;
	}

	public ProcessorDto scan() throws ConnectException{
		
		ProcessorDto processorDto = null;
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String description = Dispatch.call(item, "Name").toString();
			String family = Dispatch.call(item, "Family").toString();     
			String cores = Dispatch.call(item, "NumberOfCores").toString();      
			String clockrate = Dispatch.call(item, "MaxClockSpeed").toString();
			
			processorDto = new ProcessorDto(description, family, cores, clockrate);
			
			return processorDto;
		}
		throw new ConnectException(host + " - Could not scan Processor");
	}
	
}
	

